<template>
  <div>
    <el-button @click="back">返回</el-button>
    <el-button @click="gen">生成邀请码</el-button>
  </div>

  <el-table :data="codeList" border>
    <el-table-column prop="codeId" label="ID" width="50" />
    <el-table-column prop="codeValue" label="邀请码" width="200" />
    <el-table-column prop="userId" label="注册的用户ID" width="200" />
    <el-table-column prop="useTimeStr" label="使用时间" width="200" />
  </el-table>

</template>

<script setup lang="ts">

import {useRouter} from "vue-router";
import {onMounted, ref} from "vue";
import {genInviteCodeRequest, getInviteCodeRequest} from "../api/admin.ts";
import {messageError, messageSuccess} from "../util/message.ts";

const router = useRouter();

const back = () => {
  router.push("/user")
}

const codeList = ref<{useTime: string;useTimeStr: string;}[]>([]);

const getData = async () => {
  const response = await getInviteCodeRequest();
  codeList.value = response.data;
  for (const code of codeList.value){
    code.useTimeStr = code.useTime ? new Date(code.useTime).toLocaleString() : "未使用";
  }
}

const gen = async () => {
  const response = await genInviteCodeRequest();
  if (response.success){
    messageSuccess("生成成功");
    await getData();
  }else {
    messageError(response.message);
  }
}

onMounted(async () => {
  await getData();
})

</script>

<style scoped>

</style>